// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import router from './router'
import App from './App'
import clipper from '../static/clipper'
Vue.use(clipper)

Vue.config.productionTip = false

import Vant from 'vant';
import 'vant/lib/index.css';
import '@/assets/styles/style.less'

// 视频组件
import VueVideoPlayer from 'vue-video-player'
import 'video.js/dist/video-js.css'
Vue.use(VueVideoPlayer)

// 上下滚动组件
import scroll from 'vue-seamless-scroll'
Vue.use(scroll)

// 折线图
import echarts from 'echarts'
Vue.prototype.$echarts = echarts

// 多语言
import LangEn from '../static/lang/en'
import LangCn from '../static/lang/cn'
import LangVi from '../static/lang/vi'

import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

//Vux框架组件全局引用
import {
	Group,
	Cell,
	XButton,
	XInput,
	Toast,
	Grid,
	GridItem,
	GroupTitle,
	Confirm,
	Flexbox,
	FlexboxItem
} from 'vux'
Vue.component('group', Group)
Vue.component('cell', Cell)
Vue.component('XButton', XButton)
Vue.component('XInput', XInput)
Vue.component('Toast', Toast)
Vue.component('Grid', Grid)
Vue.component('GridItem', GridItem)
Vue.component('GroupTitle', GroupTitle)
Vue.component('confirm', Confirm)
Vue.component('flexbox', Flexbox)
Vue.component('flexbox-item', FlexboxItem)

import { ConfirmPlugin } from 'vux'
Vue.use(ConfirmPlugin)

// vux 的 Toast 窗组件
import {
	ToastPlugin
} from 'vux'
Vue.use(ToastPlugin)

// 二维码组件
import VueQriously from 'vue-qriously'
Vue.use(VueQriously)

// axios 框架
import axios from 'axios'
import Qs from 'qs'
Vue.prototype.axios = axios;
Vue.prototype.qs = Qs;

// 挂载 vant 组件
Vue.use(Vant);

// 全局函数
Vue.prototype.$develop = function() {
	this.$vux.toast.text('正在开发中,敬请期待!');
}

// 语言设置
var leng;
if(localStorage.leng == 1){
  leng = 'cn';
  axios.defaults.headers.common['Language'] = 'zh_CN';
}else if(localStorage.leng == 2 || localStorage.leng == undefined){
  leng = 'en';
  axios.defaults.headers.common['Language'] = 'en_US';
}else if(localStorage.leng == 3){
  leng = 'vi';
  axios.defaults.headers.common['Language'] = 'vi_VN';
}
const i18n = new VueI18n({
  locale: leng || 'en', //定义默认语言
  messages:{
    'cn': LangCn, // 中文语言包
    'en': LangEn, // 英文语言包,
    'vi': LangVi // 越南语言包
  }
})

// 统一的提示层组件
import { LoadingPlugin } from 'vux';
Vue.use(LoadingPlugin);
import { Notify } from 'vant';
Vue.use(Notify);

// 页面权限
router.beforeEach((to, from, next) => {
	if(to.matched.some(item => item.meta.requiresAuth)) {
		// 当 token 存在证已经登录，设置登录后的请求头，否则进入登录页面
		if(sessionStorage.token) {
			axios.defaults.headers['Authorization'] = 'bearer ' + sessionStorage.token;
			next();
		} else {
			next({
				path: '/Login'
			});
		}
	} else {
		if(to.path == '/Register' || to.path == '/FindPwd' || to.path == '/Login') {
			axios.defaults.headers['Authorization'] = '';
		}
		next();
	}
	// 响应拦截（配置请求回来的信息）
	axios.interceptors.response.use(function(response) { // 处理响应数据
		// 判断如果请求返回1004 3301 4004 5501状态码，即登录超时，清除token并跳回登录页
		if(response.data.code == '1004' || response.data.code == '3301' || response.data.code == '5501') {
			Notify({
				message: response.data.msg,
				duration: 1000,
				background: '#1989fa'
			});
			sessionStorage.clear();
			next({
				path: '/Login'
			});
		}
		return response;
	}, function(error) { // 处理响应失败
		setTimeout(() => {
			if(error.response.status == '404') {
				Notify({
					message: '404 请求接口地址错误',
					duration: 1000,
					background: '#1989fa'
				});
			} else if(error.response.status == '405') {
				Notify({
					message: '405 请求类型错误',
					duration: 1000,
					background: '#1989fa'
				});
			} else if(error.response.status == '500') {
				Notify({
					message: error.response.data.msg,
					duration: 1000,
					background: 'rgb(255, 68, 68)'
				});
			} else if(error.response.status == '504') {
				Notify({
					message: '504 网关错误',
					duration: 1000,
					background: 'rgb(255, 68, 68)'
				});
			} else {
				Notify({
					message: error.response.status + ' 其他错误：' + error.response.data.msg,
					duration: 1000,
					background: 'rgb(255, 68, 68)'
				});
			}
		}, 1000);
		return Promise.reject(error);
	});
});

// 加密
// 正常加密
Vue.prototype.rsaEncrypt = function(text) {
	let pubkey =
		"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxnhv0xsWaLUc+WZleQFHznWbDUeWHeRVH9lClgm5SVUjCAGuD1GZHZlrojMrhwG65eeYM1NtmhZ96nwzL2OV5SgxFgEscHjbExdvVNckw4mYUlyL8QyhnSEQvgHUokcQcBt9oJZMajnmwyDbPTFOUxmTnongFksrbTeJf6XUW5QIDAQAB";
	// java后台生成的
	let encrypt = new JSEncrypt();
	encrypt.setPublicKey(pubkey);
	// 加密
	return encrypt.encrypt(text)
};
// 超长加密
Vue.prototype.encryptLong = function(text) {
	let pubkey =
		"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxnhv0xsWaLUc+WZleQFHznWbDUeWHeRVH9lClgm5SVUjCAGuD1GZHZlrojMrhwG65eeYM1NtmhZ96nwzL2OV5SgxFgEscHjbExdvVNckw4mYUlyL8QyhnSEQvgHUokcQcBt9oJZMajnmwyDbPTFOUxmTnongFksrbTeJf6XUW5QIDAQAB";
	// java后台生成的
	let encrypt = new JSEncrypt();
	encrypt.setPublicKey(pubkey);
	// 加密
	return encrypt.encryptLong(text)
};

/* eslint-disable no-new */
new Vue({
	router,
	i18n,
	render: h => h(App)
}).$mount('#app-box')
